package com.iemes.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import org.iherus.codegen.qrcode.QrcodeGenerator;
import org.iherus.codegen.qrcode.SimpleQrcodeGenerator;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;

/**
 * excel工具类
 *
 * @author zhy
 * @date
 */

public class QRexcelUtil {


    /**
     * 导出excel
     *
     * @param response HttpServletResponse
     * @param fileName 文件名字
     * @param workbook 通过exportPicture()方法获取
     * @author zhy
     */
    public static void writeExcel(HttpServletResponse response, String fileName, Workbook workbook) {
        // 判断数据
        if (workbook == null) {
            System.out.println("错误!");
        }
        // 重置响应对象
        response.reset();
        try {
            OutputStream outputStream = getOutputStream(fileName, response);
            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(outputStream);
            workbook.write(bufferedOutPut);
            bufferedOutPut.flush();
            bufferedOutPut.close();
            outputStream.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 导出文件时为Writer生成OutputStream
     *
     * @param fileName 文件名字
     * @param response response
     * @return 输出流
     * @author zhy
     */
    public static OutputStream getOutputStream(String fileName, HttpServletResponse response) {
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf8");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
            response.setHeader("Pragma", "public");
            response.setHeader("Cache-Control", "no-store");
            response.addHeader("Cache-Control", "max-age=0");
            return response.getOutputStream();
        } catch (IOException e) {
            System.out.println("导出excel表格失败!");
        }
        return null;
    }

    /**
     * 获取Workbook对象
     *
     * @param pigList 查询黑猪对象列表
     * @return 文件下载
     * @author zhy
     */
//    public static Workbook exportPicture(List<QRcode> pigList) {
//        List<QRcodeModel> list = new ArrayList<>();
//        pigList.forEach(item -> {
//            QRcodeModel e = new QRcodeModel();
//            e.setEarTag(item.getEarTag());
//            String s = item.getEarTag();
//
//            QrcodeGenerator generate = new SimpleQrcodeGenerator().generate(s);
//            BufferedImage image = generate.getImage();
//            byte[] pngs = imageToBytes(image);
//            e.setQrCode(pngs);
//            list.add(e);
//        });
//        return ExcelExportUtil.exportExcel(new ExportParams(), QRcodeModel.class, list);
//    }

    /**
     * BufferedImage转byte[]
     *
     * @param bImage BufferedImage对象
     * @return byte[]
     * @auth zhy
     */
    public static byte[] imageToBytes(BufferedImage bImage) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ImageIO.write(bImage, "png", out);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return out.toByteArray();
    }
}